package com.youth.config.mq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * @ClassName RMQMessageListener
 * @Description: TODO 消息处理类。
 * @Author yjwu
 * @Date 2019/11/30 23:16
 **/
@Component
@Slf4j
public class RMQMessageListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        log.info("CommodityToOrderRMQListener start````````");
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            byte[] body = message.getBody();
            String s = new String(body, "UTF-8");
            log.info("CommodityToOrderRMQListener 监听到消息：", s);
            //确认消息被消费
            channel.basicAck(tag, true);

        } catch (Exception e) {
            e.printStackTrace();
            log.error("CommodityToOrderRMQListener发生异常：", e.fillInStackTrace());
            //确认消息被消费
            channel.basicReject(tag, false);
        }
    }
}
